package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDsihMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDsihMapper setmealDsihMapper;

    /**
     * 添加
     *
     * @param dishDTO
     */
    @Override
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();

        //将Dto 转换成普通属性实用 sprig提供的工具
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.inset(dish);
        //口味
        Long dishID = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor ->
            {
                dishFlavor.setDishId(dishID);
            });
        }
        //向口味表插入n条数据
        dishFlavorMapper.insertBatch(flavors);

    }


    @Override
    public PageResult PageQuery(DishPageQueryDTO dishPageQueryDTO) {
        //实用 mybatis 提供的 分类工具 pagehlep 输入参数 页面 和  每页显示记录数
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());

        //查询sql语句返回菜品信息
        Page<DishVO> dishVOS = dishMapper.pageQuery(dishPageQueryDTO);

        Long total = dishVOS.getTotal();
        List<DishVO> result = dishVOS.getResult();

        return new PageResult(total, result);

    }

    /**
     * 菜品状态跟新
     *
     * @param status
     * @param id
     */
    @Override
    public void UpdateStatus(Integer status, Long id) {
        Dish dish = new Dish();

        dish.setStatus(status);
        dish.setId(id);

        dishMapper.Update(dish);
    }

    /**
     * 删除菜品
     *
     * @param ids
     */
    @Override
    @Transactional //保持数据的一致性
    public void Delete(List<Long> ids) {
        //判断菜品是否在起售状态
        for (Long id : ids) {
            Dish dish = dishMapper.getByid(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                //起售中不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断当前彩品是否有被套餐关联
        List<Long> setmealTds = setmealDsihMapper.getSetealIdsByDishIds(ids);
        if (setmealTds != null && setmealTds.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品
        dishMapper.deleteByids(ids);
        //删除菜品口味
        dishFlavorMapper.deleteByIds(ids);
    }


    @Override
    public DishVO GetDishById(Long id) {
        Dish dish = dishMapper.getByid(id);
        //获取口味集合
        List<DishFlavor> dishFlavor = dishFlavorMapper.getByDishId(id);
        System.out.println("口味:" + dishFlavor);

        DishVO dishVO = new DishVO();
        //实用spring的工具复制属性
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavor);

        return dishVO;
    }


    /**
     * 修改菜品信息
     *
     * @param dishDTO
     */
    @Override
    public void Update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);


        //修改菜品信息
        dishMapper.Update(dish);

        //删除菜品口味
        dishFlavorMapper.deleteByDishId(dishDTO.getId());

        //修改菜品口味 实际就是插入新的
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());  //给口味的数据附上菜品id

            });
            //插入
            dishFlavorMapper.insertBatch(flavors);
        }


    }

    /**
     * 根据分类id查询菜品
     * @param id
     * @return
     */
    @Override
    public List<Dish> getCategoryId(Long id) {
        List<Dish> dish = dishMapper.getCategoryId(id);
        return dish;
    }


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();
        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
